<template>
  <div class="box">
    <!-- 递归父组件内容，comment -->
    <div class="comment">
      <!-- 用户信息 -->
      <div class="info">
        <!-- 头像图片 -->
        <img
          v-if="commentData.user.head_img"
          :src="$axios.defaults.baseURL+commentData.user.head_img"
          alt
        />
        <!-- 默认图片 -->
        <img v-else src="@/assets/logo.png" alt />
        <div class="user">
          <!-- 昵称 -->
          <div class="nickname">{{commentData.user.nickname}}</div>
          <!-- 事件 -->
          <div class="date">{{ commentData.create_date.split('T')[0]}}</div>
        </div>
        <!-- 右侧回复 -->
        <div class="right" @click="callReply">回复</div>
      </div>
    </div>
    <!-- 调用递归子组件 parent 
    parentDepth:深度
    -->
    <parent
      class="nbg"
      :parentDepth="parentDepth"
      :parentData="commentData.parent"
      @parentCallReply="parentCallReply"
      v-if="commentData.parent"
    ></parent>

    <!-- 第一层文字 -->
    <div class="font_color">{{commentData.content}}</div>
  </div>
</template>

<script>
//引入组件B
import parent from "../recursion/parent";
export default {
  components: {
    parent
  },
  props: ["commentData"],
  computed: {
    parentDepth() {
      let depth = 0;
      let current = this.commentData;
      while (current.parent) {
        depth += 1;
        current = current.parent;
      }
      // depth:  深度
      return depth;
    }
  },
  //方法
  methods: {
    callReply() {
      this.$emit("callReply", {
        id: this.commentData.id,
        nickname: this.commentData.user.nickname
      });
    },
    parentCallReply(parentInfo) {
      this.$emit("callReply", parentInfo);
      console.log(parentInfo);
    }
  }
};
</script>

<style lang="less" scoped>
//最外层内容
.font_color {
  padding: 2.778vw 2.778vw 1.389vw;
  font-size: 5.556vw;
  color: #000;
}
//子组件边距
.nbg {
  padding: 2.778vw;
}
//4.评论列表页
.box {
  .comment {
    padding: 2.778vw;

    //头部-回帖
    .reply {
      padding: 2.778vw;
      text-align: center;
      font-size: 5vw;
      color: #333;
    }
    //用户
    .info {
      display: flex;
      align-items: center;
      //图片
      img {
        width: 10vw;
        height: 10vw;
        border-radius: 50%;
      }
      .user {
        flex: 1;
        padding-left: 1.667vw;
        //昵称
        .nickname {
          font-size: 3.889vw;
          color: #333;
          padding-bottom: 1.111vw;
        }
        //时间
        .date {
          font-size: 3.333vw;
          color: #aaa;
        }
      }
      //回复按钮
      .right {
        font-size: 3.333vw;
        color: #333;
      }
    }
  }
}
</style>